import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:ride_rex/generated/assets.dart';
import 'package:ride_rex/login/login_logic.dart';
import 'package:ride_rex/route.dart';
import 'package:ride_rex/standard/app_colors.dart';
import 'package:ride_rex/ui/common_dialogs.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class LoginPage extends GetView<LoginLogic> {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      scaffoldBackgroundColor: Colors.white,
      child: GetBuilder<LoginLogic>(
        init: LoginLogic(),
        assignId: true,
        builder: (logic) {
          return SizedBox(
            height: ScreenUtil().screenHeight,
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                _buildBg(),
                _buildLoginContainer().positioned(bottom: 25.w + ScreenUtil().bottomBarHeight * 0.5),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildBg() {
    return const FractionallySizedBox(
      widthFactor: 1,
      child: AspectRatio(
        aspectRatio: 0.714,
        child: IconWidget.image(
          Assets.pngLoginBg,
          fit: BoxFit.fitWidth,
        ),
      ),
    );
  }

  Widget _buildLoginContainer() {
    return Container(
      decoration: CommonDecoration.getShapeDecoration(
        borderColor: '#05A6FD'.toColor,
        radius: 12.w,
        borderWidth: 1,
        bg: Colors.white,
      ),
      width: 310.w,
      padding: EdgeInsets.fromLTRB(22.w, 8.w, 22.w, 20.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildTitle(),
          _buildFastLogin().paddingTop(17.w),
          _buildAppleLogin().paddingTop(28.w),
          _buildPrivacy(),
        ],
      ),
    );
  }

  Widget _buildPrivacy() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        ButtonWidget.icon(
          IconWidget.image(controller.agree ? Assets.pngRadioSelect : Assets.pngRadioNormal, size: 16.w),
          padding: EdgeInsets.all(8.w),
          hasInkWell: false,
          onTap: () {
            controller.agree = !controller.agree;
            controller.update();
          },
        ),
        Text.rich(
          TextSpan(
            text: 'By using our App you agree with our ',
            children: [
              TextSpan(
                text: 'Term of Use',
                style: const TextStyle(color: AppColors.primary),
                recognizer: TapGestureRecognizer()..onTap = CommonRoute.openProtocolPage,
              ),
              const TextSpan(text: ' and '),
              TextSpan(
                text: 'Privacy Policy.',
                style: const TextStyle(color: AppColors.primary),
                recognizer: TapGestureRecognizer()..onTap = CommonRoute.openPrivacyPage,
              ),
            ],
          ),
          style: const TextStyle(color: AppColors.text, fontSize: 14, height: 1.5),
        ).paddingTop(13.w).expanded(),
      ],
    );
  }

  Widget _buildTitle() => Assets.svgLoginWelcome.toSvg(width: 214.w);

  Widget _buildFastLogin() => ButtonWidget(
        gradient: AppColors.primaryGradient,
        width: 263.w,
        height: 54.w,
        borderRadius: 12.w,
        onTap: () => controller.fastLogin(),
        child: Row(
          children: [
            Assets.svgLoginFast.toSvg(width: 24.w).padding(left: 32.w, right: 37.w),
            const TextWidget.oneLine(text: 'Fast Login', fontSize: 16, color: Colors.white),
          ],
        ),
      );

  Widget _buildAppleLogin() => ButtonWidget(
        width: 263.w,
        height: 54.w,
        backgroundColor: '#65616E'.toColor,
        borderRadius: 12.w,
        onTap: controller.appleLogin,
        child: Row(
          children: [
            Assets.svgLoginApple.toSvg(width: 24.w).padding(left: 32.w, right: 37.w),
            const TextWidget.oneLine(text: 'Sign in with Apple', fontSize: 16, color: Colors.white),
          ],
        ),
      );
}
